<?php

// 参考：http://www.php.net/manual/zh/pdo.connections.php
// https://segmentfault.com/a/1190000004106175

include_once 'db_config.php';
try {
// array(PDO::ATTR_PERSISTENT => true) 开启持久化连接
    $dbh = new PDO('mysql:host=localhost;dbname=wx', $db['user'], $db['pwd'], array(PDO::ATTR_PERSISTENT => true));

    if (empty($_POST["date_start"]) & empty($_POST["date_end"])) {
        // 获取指定日期的净增用户总量
        $next_day = date("Y-m-d", strtotime('-14 days'));
        $end_day = date('Y-m-d', strtotime('-1 days'));
    } else {
        $next_day = $_POST['date_start'];
        $end_day = $_POST['date_end'];
    }
    $sql = 'SELECT * FROM `usercumulate` where ref_date>="' . $next_day . '" and ref_date<="' . $end_day . '"';
    foreach ($dbh->query($sql) as $row) {
        // 把日期和用户数单独组成列 | 变量追加到数组中
        $arr['ref_date'][] = $row['ref_date'];
        $arr['cumulate_user'][] = $row['cumulate_user'];
    }

    $next_day = date("Y-m-d", strtotime("+1 days", strtotime($next_day)));
    // 把数据转换成json格式
    $data = json_encode($arr);
    echo $data;

    // 现在运行完成，在此关闭连接
    $dbh = null;
} catch (PDOException $e) {
    print "Error!: " . $e->getMessage() . "<br/>";
    die();
}
?>